package com.joeyzh;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.joey.base.util.FileUtil;
import com.joey.base.util.LogUtils;
import com.joey.base.util.SharedPreferenceUtils;
import com.joey.ui.widget.ToastHelper;
import com.joeyzh.iframe.db.DBManagerUtil;
import com.joeyzh.utils.AppConsts;

import java.io.File;


/**
 * Created by Administrator on 2016/12/1.
 *
 * @author Joeyzh
 */
public class BaseApplication extends Application {
    private static BaseApplication mAppInstance;
    //SD卡根路径
    private static final String mSdRootPath = Environment.getExternalStorageDirectory().getPath();

    private Context mAppContext;

    public static BaseApplication getInstance() {
        return mAppInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.a("onCreate init");
        mAppInstance = this;
        initSDKS();
        // 创建文件路径
        createDir();
        // 初始化服务器
        initServer();
        LogUtils.a("onCreate ok");
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }

    /**
     * TODO 加载三方SDK
     */
    private void initSDKS() {
        SharedPreferenceUtils.getInstance().init(this, AppConsts.APP_NAME);
        new Thread() {
            @Override
            public void run() {
                super.run();
                DBManagerUtil.getInstance().init(getApplicationContext());
            }
        }.start();
    }

    public static void createDir() {
        FileUtil.createDirectory(new File(AppConsts.Path.mDataRootPath));
        FileUtil.createDirectory(new File(AppConsts.Path.CACHE_PATH));
        FileUtil.createDirectory(new File(AppConsts.Path.IMG_PATH));
        FileUtil.createDirectory(new File(AppConsts.Path.DOWNLOAD_PATH));
        FileUtil.createDirectory(new File(AppConsts.Path.UPDATE_PATH));
    }

    private void initServer() {
        String host = SharedPreferenceUtils.getInstance().getString("host");
        String port = SharedPreferenceUtils.getInstance().getString("port");
        String server = SharedPreferenceUtils.getInstance().getString("server");
        AppConsts.Url.setServer(host, port, server);
        if (BuildConfig.DEBUG) {
            ToastHelper.show(this, AppConsts.Url.getDomainRoot());
        }
    }
}
